package cn.xspace.wnx.view.wnxchat;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.DateUtils;

import cn.xspace.library.util.MyInfoUtil;

import java.text.ParseException;
import java.util.Map;

import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.WNXBean;
import cn.xspace.wnx.view.chat.ChatMessageList;
import rx.Subscription;


public abstract class BaseWnxChatRow extends LinearLayout {
    protected static final String TAG = BaseWnxChatRow.class.getSimpleName();
    protected Subscription subscription;
    protected LayoutInflater inflater;
    protected Context context;
    protected BaseAdapter adapter;
    protected WNXBean wnxBean;
    protected int position;

    protected Activity activity;

    protected String userId = "";
    protected Map<String, Object> sendMap;
    protected ChatMessageList.MessageListItemClickListener itemClickListener;

    public BaseWnxChatRow(Context context, WNXBean wnxBean, int position, BaseAdapter adapter) {
        super(context);
        userId = MyInfoUtil.getMyUserid(context);
        this.context = context;
        this.wnxBean = wnxBean;
        this.activity = (Activity) context;
        this.position = position;
        this.adapter = adapter;
        inflater = LayoutInflater.from(context);
        initView();
    }


    protected void initView() {
        onInflateView();
        onFindViewById();
    }


    /**
     * set property according message and postion
     *
     * @param wnxBean
     * @param position
     */
    public void setUpView(WNXBean wnxBean, int position) {
        this.wnxBean = wnxBean;
        this.position = position;
        setUpBaseView();
        onSetUpView();
        setClickListener();
    }

    private void setUpBaseView() {
        // set nickname, avatar and background of bubble
        TextView timestamp = (TextView) findViewById(R.id.timestamp);
        if (timestamp != null) {
            if (position == 0) {
                timestamp.setText(DateUtils.getChatDatetimeDesc(wnxBean.msg_send_time));
                timestamp.setVisibility(View.VISIBLE);
            } else {
                // show time stamp if interval with last message is > 30 seconds
                WNXBean prevMessage = (WNXBean) adapter.getItem(position - 1);

//                if (prevMessage != null && DateUtils.isCloseEnough(message.msg_last_time, prevMessage.msg_last_time)) {
                if (prevMessage != null && !isShowTimeIfNeed(wnxBean.msg_send_time, prevMessage.msg_send_time)) {
                    timestamp.setVisibility(View.GONE);
                } else {
                    timestamp.setText(DateUtils.getChatDatetimeDesc(wnxBean.msg_send_time));
                    timestamp.setVisibility(View.VISIBLE);
                }
            }
        }
    }


    private boolean isShowTimeIfNeed(String timeNow, String previousMsg) {
        long chatTime = 0;
        try {
            chatTime = DateUtils.stringToDate(timeNow, null).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long previousChatTime = 0;
        if (previousMsg != null) {
            try {
                previousChatTime = DateUtils.stringToDate(
                        previousMsg, null).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return (chatTime - previousChatTime > 5 * 60 * 1000);// 超过五分钟显示聊天时间
    }

    protected void setClickListener() {
    }


    protected void updateView() {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                onUpdateView();
            }
        });

    }

    protected abstract void onInflateView();

    /**
     * find view by id
     */
    protected abstract void onFindViewById();

    /**
     * refresh list view when message status change
     */
    protected abstract void onUpdateView();

    /**
     * setup view
     */
    protected abstract void onSetUpView();

    /**
     * on bubble clicked
     */
    protected abstract void onBubbleClick();

}
